/**
 * StrUtil.java 
 * Copyright © 2015-3015, 杭州泊享网络科技有限公司
 * 
 * @author Liu Tao
 * @create 2013年8月26日
 */
package com.pshare.util;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

import com.pshare.constant.ReturnMsg;
import com.pshare.constant.SeaxConstants;

/**
 * 字符串格式校验
 */
public class StrCheckUtils {

	private static final String UNDEFINED = "undefined";

	/**
	 * 判断输入是否为空或未定义
	 */
	public static boolean isBlank(String str) {
		return StringUtils.isBlank(str) || UNDEFINED.equalsIgnoreCase(str);
	}

	public static boolean isNotBlank(String str) {
		return StringUtils.isNotBlank(str) && !UNDEFINED.equalsIgnoreCase(str);
	}

	/**
	 * 校验邮件地址格式
	 */
	public static boolean isEmail(String email) {
		if (StringUtils.isBlank(email)) {
			return false;
		}
		// Pattern pattern =
		// Pattern.compile("^/w+([-.]/w+)*@/w+([-]/w+)*/.(/w+([-]/w+)*/.)*[a-z]{2,3}$");
		Pattern pattern = Pattern.compile("^[a-zA-Z0-9_.]+@[a-zA-Z0-9-]+[.a-zA-Z]+$");
		Matcher matcher = pattern.matcher(email);
		if (matcher.matches()) {
			return true;
		}
		return false;
	}

	/**
	 * 身份证校验
	 * 
	 * @param cardNum
	 * @return boolean
	 */
	public static boolean checkIdentityCard(String cardNum) {
		Pattern pattern = Pattern.compile("^\\d{15}$|^\\d{18}$|^\\d{17}\\d|X|x$");
		Matcher matcher = pattern.matcher(cardNum);
		if (matcher.matches()) {
			return true;
		}
		return false;
	}

	/**
	 * 银行卡号校验
	 * 
	 * @param cardNum
	 * @return boolean
	 */
	public static boolean checkBankCardNum(String cardNum) {
		if (StringUtils.isBlank(cardNum)) {
			return false;
		}
		cardNum = cardNum.replaceAll(SeaxConstants.BLANK, SeaxConstants.EMPTY);
		if (!ArithUtils.isNumber(cardNum)) {
			return false;
		}
		if (cardNum.length() < 14 || cardNum.length() > 19) {
			return false;
		}
		return true;
	}

	public static Map<String, Object> checkPwd(String password) {
		Map<String, Object> result = new HashMap<String, Object>();

		if (StringUtils.isBlank(password)) {
			result.put(SeaxConstants.KEY_RESULT, false);
			result.put(SeaxConstants.KEY_MSG, ReturnMsg.EMPTY_PWD);
			return result;
		}
		if (password.indexOf(SeaxConstants.BLANK) > -1) {
			result.put(SeaxConstants.KEY_RESULT, false);
			result.put(SeaxConstants.KEY_MSG, ReturnMsg.PWD_CONTAIN_BLANK);
			return result;
		}
		if (password.length() < 6 || password.length() > 20) {
			result.put(SeaxConstants.KEY_RESULT, false);
			result.put(SeaxConstants.KEY_MSG, ReturnMsg.PWD_LENGTH);
			return result;
		}
		result.put(SeaxConstants.KEY_RESULT, true);
		return result;
	}

	public static boolean checkImei(String imei) {
		Pattern pattern = Pattern.compile("^\\d{15}$");
		Matcher matcher = pattern.matcher(imei);
		if (matcher.matches()) {
			return true;
		}
		return false;
	}

	public static boolean checkVerifyCode(String code) {
		code = code.replaceAll(SeaxConstants.BLANK, SeaxConstants.EMPTY);
		Pattern pattern = Pattern.compile("^\\w{4}\\d{8}$");
		Matcher matcher = pattern.matcher(code);
		if (matcher.matches()) {
			return true;
		}
		return false;
	}

	/**
	 * 从帖子中读取被@的用户ID列表
	 * 
	 * @param content
	 * @return Map<Long, String>
	 */
	public static Map<Long, String> getUserIdsFromContentAt(String content) {
		Map<Long, String> userMap = new HashMap<Long, String>();
		if (StringUtils.isBlank(content)) {
			return userMap;
		}

		Pattern pattern = Pattern.compile("@\\[\\d{1,11}\\:[(a-zA-Z\\u4e00-\\u9fa5)]{1,32}\\]");
		Matcher matcher = pattern.matcher(content);
		while (matcher.find()) {
			try {
				String atStr = matcher.group();
				int pos = atStr.indexOf(SeaxConstants.COLON);
				Long userId = Long.parseLong(atStr.substring(2, pos));
				String userName = atStr.substring(pos + 1, atStr.length() - 1);
				if (!userMap.containsKey(userId)) {
					userMap.put(userId, userName);
				}
			} catch (Exception e) {
				throw new RuntimeException("格式有误");
			}
		}

		return userMap;
	}

}
